package com.example.demo6.ui.edit;

import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.SQLite.PickupGoods;

public class edit_mypickupfragment extends Fragment {

    private EditText editMypickupItem,editMypickupPlace,editMypickupPhone,editMypickupTime,editMypickupItemDetail;
    private Button UpdateMypickupBtn,CancelMypickupBtn,DeleteMypickupBtn;
    PickupGoods pickupGoods;
    private EditMylostfragmentViewModel mViewModel;
    private NavController navController;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_mypickupfragment, container, false);

        // 初始化UI组件
        editMypickupItem = view.findViewById(R.id.editMypickupItem);
        editMypickupPlace = view.findViewById(R.id.editMypickupPlace);
        editMypickupPhone = view.findViewById(R.id.editMypickupPhone);
        editMypickupTime = view.findViewById(R.id.editMypickupTime);
        editMypickupItemDetail = view.findViewById(R.id.editMypickupItemDetail);
        UpdateMypickupBtn = view.findViewById(R.id.UpdateMypickupBtn);
        CancelMypickupBtn = view.findViewById(R.id.CancelMypickupBtn);
        DeleteMypickupBtn = view.findViewById(R.id.DeleteMypickupBtn);

        // 从Bundle中获取goodsId
        int PickupId = getArguments() != null ? getArguments().getInt("PickupId") : -1;
        if (PickupId != -1) {
            new SelectMyPickupGoodsTask(PickupId).execute();
        }

        // 设置更新按钮的点击监听器
        if (UpdateMypickupBtn != null) {
            UpdateMypickupBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateMyPickupGoods();
                }
            });
        }

        // 设置取消按钮的点击监听器
        if (CancelMypickupBtn != null) { // 添加空值检查
            CancelMypickupBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
                    navController.popBackStack();
                }
            });
        }
        // 设置删除按钮的点击监听器
        if (DeleteMypickupBtn != null) {
            DeleteMypickupBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteMyPickupGoods();
                }
            });
        }

        return view;
    }

    private void updateMyPickupGoods() {
        // 检查pickupGoods是否已加载
        if (pickupGoods != null) {
            // 更新LostGoods对象的字段
            pickupGoods.setPickupGoods(editMypickupItem.getText().toString());
            pickupGoods.setPickupPlace(editMypickupPlace.getText().toString());
            pickupGoods.setPickupPhone(editMypickupPhone.getText().toString());
            pickupGoods.setPickupTime(editMypickupTime.getText().toString());
            pickupGoods.setPickupDetail(editMypickupItemDetail.getText().toString());

            // 执行更新操作
            new UpdateMyPickupGoodsTask().execute(pickupGoods);
        } else {
            Toast.makeText(getContext(), "数据加载失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteMyPickupGoods() {
        // 检查pickupGoods是否已加载
        if (pickupGoods != null) {
            // 执行删除操作
            new DeleteMyPickupGoodsTask().execute(pickupGoods.getPickupId());
        } else {
            Toast.makeText(getContext(), "数据加载失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
    @SuppressLint("StaticFieldLeak")
    private class SelectMyPickupGoodsTask extends AsyncTask<Void, Void, Void>
    {
        int pickupId;
        public SelectMyPickupGoodsTask(int pickupId)
        {
            this.pickupId=pickupId ;
        }
        //不能在这里更新UI,否则有异常
        @Override
        protected Void doInBackground(Void... arg0)
        {
            pickupGoods= MyDatabase.getInstance(getContext()).pickupGoodsDao().getMyPickupGoodsByPickupId(pickupId);
            return null;
        }
        //onPostExecute用于doInBackground执行完后，可以更新界面UI。
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            // 绑定数据到视图
            bindDataToView(pickupGoods);
        }
    }
    //更新
    @SuppressLint("StaticFieldLeak")
    private class UpdateMyPickupGoodsTask extends AsyncTask<PickupGoods, Void, Void> {
        @Override
        protected Void doInBackground(PickupGoods... pickupGoods) {
            MyDatabase.getInstance(getContext()).pickupGoodsDao().updateMyPickupGoods(pickupGoods[0]);
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Toast.makeText(getContext(), "更新成功！", Toast.LENGTH_SHORT).show();
            NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
            navController.popBackStack();
        }
    }
    //删除
    @SuppressLint("StaticFieldLeak")
    private class DeleteMyPickupGoodsTask extends AsyncTask<Integer, Void, Void> {
        @Override
        protected Void doInBackground(Integer... integers) {
            MyDatabase.getInstance(getContext()).pickupGoodsDao().deleteMyPickupGoodsById(integers[0]);
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Toast.makeText(getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
            NavController navController = Navigation.findNavController((Activity) getContext(), R.id.nav_host_fragment_activity_main);
            navController.popBackStack();
        }
    }
    private void bindDataToView(PickupGoods pickupGoods) {
        editMypickupItem.setText(pickupGoods.getPickupGoods());
        editMypickupPlace.setText(pickupGoods.getPickupPlace());
        editMypickupPhone.setText(pickupGoods.getPickupPhone());
        editMypickupTime.setText(pickupGoods.getPickupTime());
        editMypickupItemDetail.setText(pickupGoods.getPickupDetail());
    }
}
